package com.hxzy.controller.vue.merchandiser;

import cn.hutool.crypto.digest.BCrypt;
import com.github.pagehelper.PageInfo;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.resp.PageResp;
import com.hxzy.common.resp.R;
import com.hxzy.common.util.MarketingThreadLocalUtil;
import com.hxzy.common.util.TencentSmsUtil;
import com.hxzy.common.wrap.PageInfoUtil;
import com.hxzy.controller.vue.login.resp.AdminLoginResp;
import com.hxzy.controller.vue.mapstruct.MerchandiserConvert;
import com.hxzy.controller.vue.merchandiser.req.MerchandiserPwdReq;
import com.hxzy.controller.vue.merchandiser.req.MerchandiserReq;
import com.hxzy.controller.vue.merchandiser.req.MerchandiserUpdateReq;
import com.hxzy.controller.vue.merchandiser.req.ProcessReq;
import com.hxzy.controller.vue.merchandiser.resp.MerchandiserResp;
import com.hxzy.entity.Merchandiser;
import com.hxzy.service.MerchandiserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @Author wy
 * @Description 商家信息API
 * @Date 2023/9/7 10:55
 * @Version 1.0
 */
@RequestMapping(value = "/api/merchandiser")
@RestController
@Api(tags = "后台-商家")
public class MerchandiserController {

    @Autowired
    private MerchandiserService merchandiserService;


    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/data")
    public R<PageResp<MerchandiserResp>>  search( @Valid MerchandiserReq req){
        PageInfo<MerchandiserResp> pageInfo= this.merchandiserService.search(req);
        return R.okHasData( PageInfoUtil.pageInfoToResp(pageInfo));
    }


    @ApiOperation(value = "管理员审核商家信息")
    @PostMapping(value = "/process")
    public R  examine(@RequestBody  @Valid ProcessReq req){
        boolean result=this.merchandiserService.process(req);

        Merchandiser db = this.merchandiserService.getById(req.getTenantId());
        //根据管理员提交状态
        if(req.getStatus().equals("1")){
            //通过短信提示  (如果失败了，要进行记录下来，以后手动补偿)
            TencentSmsUtil.approvalPass( db.getLoginName());
        }else{
            //不通短信提示 (如果失败了，要进行记录下来，以后手动补偿)
            TencentSmsUtil.approvalNoPass(db.getLoginName(),req.getDescription());
        }
        return result?R.ok(): R.build(AckCode.FAIL);
    }


    @ApiOperation(value = "获取登录商家的详细信息")
    @GetMapping(value = "/detail")
    public R<MerchandiserResp> detail() {
        //从ThreadLocal中获取登录商家的id
        AdminLoginResp loginResp = MarketingThreadLocalUtil.adminThreadLocal.get();
        //查询商家信息
        Merchandiser merchandiser = this.merchandiserService.getById(loginResp.getId());
        MerchandiserResp convert = MerchandiserConvert.INSTANCE.convert(merchandiser);
        return R.okHasData(convert);
    }

    //商家修改信息
    @ApiOperation(value = "商家修改信息")
    @PutMapping(value = "/update")
    public R update(@RequestBody @Valid MerchandiserUpdateReq req) {
        //判断是否是自己账号
        AdminLoginResp loginResp = MarketingThreadLocalUtil.adminThreadLocal.get();
        //得到id
        Long id = loginResp.getId();
        //进行比较
        if(!id.equals(req.getId())){
            return R.build(AckCode.NOT_MODIFY_OTHER_USER);
        }
        //转换为merchandiser实体类
        Merchandiser convert = MerchandiserConvert.INSTANCE.convert(req);
        //更新数据
        boolean result = this.merchandiserService.updateById(convert);
        return result ? R.ok() : R.build(AckCode.DATABASE_ERROR);
    }



    @ApiOperation(value = "商家修改密码")
    @PostMapping(value = "/updatePwd")
    public R updatePwd(@RequestBody @Valid MerchandiserPwdReq req) {
        //判断是否是自己账号
        AdminLoginResp loginResp = MarketingThreadLocalUtil.adminThreadLocal.get();
        //得到id
        Long id = loginResp.getId();
        //进行比较
        if(!id.equals(req.getId())){
            return R.build(AckCode.NOT_MODIFY_OTHER_USER);
        }

        //新密码与确认密码对比
        if (!req.getNewPwd().equals(req.getRePwd())) {
            return R.build(AckCode.SET_PASSWORD_INVALID);
        }

        //修改密码
        Merchandiser merchandiser = new Merchandiser();
        merchandiser.setId(id);
        //对密码进行加密
        String hashpw = BCrypt.hashpw(req.getNewPwd());
        merchandiser.setLoginPwd(hashpw);

        //修改数据库
        boolean result = this.merchandiserService.updateById(merchandiser);
        return result ? R.ok() : R.build(AckCode.DATABASE_ERROR);
    }



}
